<template>
  <ElementDialog :title="!dataForm.id ? '新增' : '修改'" :visible.sync="visible">
    <ElForm
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter.native="dataFormSubmit()">
      <ElFormItem label="唯一编码（用于关联附件）" prop="unid">
        <ElInput v-model="dataForm.unid" placeholder="唯一编码（用于关联附件）" />
      </ElFormItem>
      <ElFormItem label="创建人ID" prop="createId">
        <ElInput v-model="dataForm.createId" placeholder="创建人ID" />
      </ElFormItem>
      <ElFormItem label="创建人姓名" prop="createBy">
        <ElInput v-model="dataForm.createBy" placeholder="创建人姓名" />
      </ElFormItem>
      <ElFormItem label="创建时间" prop="createTime">
        <ElInput v-model="dataForm.createTime" placeholder="创建时间" />
      </ElFormItem>
      <ElFormItem label="创建人所属OU的ID" prop="deptId">
        <ElInput v-model="dataForm.deptId" placeholder="创建人所属OU的ID" />
      </ElFormItem>
      <ElFormItem label="创建人所属OU的全称" prop="deptFullname">
        <ElInput v-model="dataForm.deptFullname" placeholder="创建人所属OU的全称" />
      </ElFormItem>
      <ElFormItem label="最后修改人信息" prop="updateBy">
        <ElInput v-model="dataForm.updateBy" placeholder="最后修改人信息" />
      </ElFormItem>
      <ElFormItem label="最后修改时间" prop="updateTime">
        <ElInput v-model="dataForm.updateTime" placeholder="最后修改时间" />
      </ElFormItem>
      <ElFormItem label="租户ID" prop="tenantId">
        <ElInput v-model="dataForm.tenantId" placeholder="租户ID" />
      </ElFormItem>
      <ElFormItem label="数据删除标记" prop="delFlag">
        <ElInput v-model="dataForm.delFlag" placeholder="数据删除标记" />
      </ElFormItem>
      <ElFormItem label="党组织ID" prop="organizationPartId">
        <ElInput v-model="dataForm.organizationPartId" placeholder="党组织ID" />
      </ElFormItem>
      <ElFormItem label="党组织名称" prop="organizationPart">
        <ElInput v-model="dataForm.organizationPart" placeholder="党组织名称" />
      </ElFormItem>
      <ElFormItem label="处分或问责类型" prop="punishAccountType">
        <ElInput v-model="dataForm.punishAccountType" placeholder="处分或问责类型" />
      </ElFormItem>
      <ElFormItem label="处分或问责单位" prop="disciplinaryAccountUnit">
        <ElInput v-model="dataForm.disciplinaryAccountUnit" placeholder="处分或问责单位" />
      </ElFormItem>
      <ElFormItem label="发文名称" prop="postName">
        <ElInput v-model="dataForm.postName" placeholder="发文名称" />
      </ElFormItem>
      <ElFormItem label="发文时间" prop="dispatchDate">
        <ElInput v-model="dataForm.dispatchDate" placeholder="发文时间" />
      </ElFormItem>
      <ElFormItem label="备注" prop="remarks">
        <ElInput v-model="dataForm.remarks" placeholder="备注" />
      </ElFormItem>
    </ElForm>
    <template #footer>
      <span class="dialog-footer">
        <ElButton @click="visible = false">取消</ElButton>
        <ElButton v-if="canSubmit" type="primary" @click="dataFormSubmit()">确定</ElButton>
      </span>
    </template>
  </ElementDialog>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/personnel/accountDisciplinaryRecords'

  export default {
    data() {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: 0,
          unid: '',
          createId: '',
          createBy: '',
          createTime: '',
          deptId: '',
          deptFullname: '',
          updateBy: '',
          updateTime: '',
          tenantId: '',
          delFlag: '',
          organizationPartId: '',
          organizationPart: '',
          punishAccountType: '',
          disciplinaryAccountUnit: '',
          postName: '',
          dispatchDate: '',
          remarks: ''
        },
        dataRule: {
          unid: [{ required: true, message: '唯一编码（用于关联附件）不能为空', trigger: 'blur' }],
          createId: [{ required: true, message: '创建人ID不能为空', trigger: 'blur' }],
          createBy: [{ required: true, message: '创建人姓名不能为空', trigger: 'blur' }],
          createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
          deptId: [{ required: true, message: '创建人所属OU的ID不能为空', trigger: 'blur' }],
          deptFullname: [{ required: true, message: '创建人所属OU的全称不能为空', trigger: 'blur' }],
          updateBy: [{ required: true, message: '最后修改人信息不能为空', trigger: 'blur' }],
          updateTime: [{ required: true, message: '最后修改时间不能为空', trigger: 'blur' }],
          tenantId: [{ required: true, message: '租户ID不能为空', trigger: 'blur' }],
          delFlag: [{ required: true, message: '数据删除标记不能为空', trigger: 'blur' }],
          organizationPartId: [{ required: true, message: '党组织ID不能为空', trigger: 'blur' }],
          organizationPart: [{ required: true, message: '党组织名称不能为空', trigger: 'blur' }],
          punishAccountType: [{ required: true, message: '处分或问责类型不能为空', trigger: 'blur' }],
          disciplinaryAccountUnit: [{ required: true, message: '处分或问责单位不能为空', trigger: 'blur' }],
          postName: [{ required: true, message: '发文名称不能为空', trigger: 'blur' }],
          dispatchDate: [{ required: true, message: '发文时间不能为空', trigger: 'blur' }],
          remarks: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
        }
      }
    },
    methods: {
      async init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.canSubmit = true
        await this.$nextTick()
        this.$refs.dataForm.resetFields()
        if (this.dataForm.id) {
          const response = await getObj(this.dataForm.id)
          this.dataForm = response.data.data
        }
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.canSubmit = false
            if (this.dataForm.id) {
              putObj(this.dataForm)
                .then(data => {
                  this.$notify.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                })
                .catch(() => {
                  this.canSubmit = true
                })
            } else {
              addObj(this.dataForm)
                .then(data => {
                  this.$notify.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                })
                .catch(() => {
                  this.canSubmit = true
                })
            }
          }
        })
      }
    }
  }
</script>
